"""
Case Type   : 资源池化
Case Name   : ss_instance_id参数验证
Create At   : 2023.10
Owner       : wx1224102
Description :
    1、查看参数，校验原始默认值，当前参数值，参数类型，取值类型及范围
    2、修改参数为超出范围值
    3、恢复原始值，恢复集群状态
Expect      :
    1、原始值为0，当前参数值主节点为0，备1节点为1，参数类型为postmaster，取值类型为integer，取值范围为0~63
    2、修改失败，合理报错
    3、恢复原始值成功，集群状态正常
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.param = 'ss_instance_id'
        self.src = self.com.show_param(self.param)

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_shared_storage(self):
        text = '-----step1:查看参数，校验默认值等; expect:启动默认值为on,当前参数值为on,' \
               '参数类型为postmaster类型,取值类型为布尔类型-----'
        self.log.info(text)

        pri_config = f'''select name,setting,context,boot_val,reset_val,
            vartype,min_val,max_val from pg_settings where name = '{self.param}';'''
        self.log.info(pri_config)
        pri_res = self.pri_sh.execut_db_sql(pri_config)
        self.log.info(pri_res)

        res1 = pri_res.splitlines()[0].split('|')
        res2 = pri_res.splitlines()[-2].split('|')
        result = dict(zip(res1, res2))
        self.log.info(result)

        value_list1 = []
        for key, values in result.items():
            value_list1.append(values.strip())
        self.log.info(value_list1)
        self.assertEqual(f'{self.param}', value_list1[0].strip(), '参数名校验失败')
        self.assertEqual('0', value_list1[1].strip(), '当前值校验失败')
        self.assertEqual('postmaster', value_list1[2].strip(), '参数类型校验失败')
        self.assertEqual('0', value_list1[3].strip(), '启动默认值校验失败')
        self.assertEqual('0', value_list1[4].strip(), '重置参数值校验失败')
        self.assertEqual('integer', value_list1[5].strip(), '参数取值类型校验失败')
        self.assertEqual('0', value_list1[6].strip(), '最小值校验失败')
        self.assertEqual('63', value_list1[7].strip(), '最大值校验失败')

        sta_config = f'''select name,setting,reset_val from pg_settings 
            where name = '{self.param}';'''
        self.log.info(sta_config)
        sta_res = self.sta_sh.execut_db_sql(sta_config)
        self.log.info(sta_res)

        res3 = sta_res.splitlines()[0].split('|')
        res4 = sta_res.splitlines()[-2].split('|')
        result2 = dict(zip(res3,res4))
        self.log.info(result2)

        value_list2 = []
        for key, values in result2.items():
            value_list2.append(values.strip())
        self.log.info(value_list2)
        self.assertEqual('1', value_list2[1].strip(), '当前值校验失败')
        self.assertEqual('1', value_list2[2].strip(), '重置参数值校验失败')

        text = '-----step2:修改参数为66; expect:修改失败，合理报错-----'
        self.log.info(text)

        set_param1 = self.pri_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.param} = 66")
        self.log.info(set_param1)
        self.assertFalse(set_param1, '参数修改成功')

    def tearDown(self):
        text = '-----step3:恢复环境为初始值，恢复集群状态; expect:恢复初始值成功，集群状态正常-----'
        self.log.info(text)
        self.log.info('-----no need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
